﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.BusinessLogic;
using DiscoveryLogic.Analytics.BusinessLogic.Dedups;
using DiscoveryLogic.Analytics.BusinessLogic.Links;
using DiscoveryLogic.Analytics.BusinessLogic.Rules;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Dedup;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.Modules.Explorers
{
    /// <summary>
    /// Interaction logic for ExploreSelectedEntity.xaml
    /// </summary>
    public partial class ExploreSelectedEntity : UserControl
    {
        public delegate void OnNetworkUpdated(Network network);
        public event OnNetworkUpdated NetworkUpdated;


        private EntityBase _SrcEntity;
        private Network _Network;
        private EntityType _TgtEntityType;
        private Dictionary<string, EntityRelationRule> _RulesByNames;

        public ExploreSelectedEntity(
            Network network, EntityBase srcEntity, 
            EntityType tgtEntityType)
        {
            InitializeComponent();

            this._Network = network;
            this._SrcEntity = srcEntity;
            this._TgtEntityType = tgtEntityType;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtNetworkName.Text = this._Network.NetworkName;
            List<RuleBase> rules = new List<RuleBase>();
            string iconPath = "/images/link.ico";
            List<RuleBase> linkRules = RuleManager.GetRulesByType(RuleType.Link, iconPath);
            if (linkRules != null && linkRules.Count > 0)
                rules.AddRange(linkRules);
            this._RulesByNames = new Dictionary<string, EntityRelationRule>();
            if (rules != null && rules.Count > 0)
            {
                foreach (RuleBase rule in rules)
                {
                    this.cboRules.Items.Add(rule.RuleName);
                    if (rule is EntityRelationRule)
                        this._RulesByNames.Add(rule.RuleName, rule as EntityRelationRule);
                }
            }

            this.txtSrcEntityType.Text = this._SrcEntity.EntityType.ToString();
            this.txtSrcEntityName.Text = this._SrcEntity.Name;
            this.cboTgtEntity.Items.Clear();
            List<EntityType> entityTypes = new List<EntityType>();
            foreach (EntityBase entity in this._Network.Records)
            {
                if (!entityTypes.Contains(entity.EntityType))
                    entityTypes.Add(entity.EntityType);
            }
            
            this.cboTgtEntity.Items.Add(EntityType.Grants.ToString());
            this.cboTgtEntity.Items.Add(EntityType.Organizations.ToString());
            this.cboTgtEntity.Items.Add(EntityType.Persons.ToString());
            this.cboTgtEntity.Items.Add(EntityType.Publications.ToString());
            this.cboTgtEntity.Text = this._TgtEntityType.ToString();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if (this.Parent is Window)
            {
                ((Window)this.Parent).Close();
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (this.cboRules.SelectedIndex >= 0 && !string.IsNullOrEmpty(this.txtNetworkName.Text))
            {
                try
                {
                    EntityRelationRule rule = this._RulesByNames[this.cboRules.SelectedItem.ToString()];
                    List<string> srcKeys = new List<string>();
                    srcKeys.Add(this._SrcEntity.PKValue());
                    EntityRelationWorker worker = new EntityRelationWorker(
                        rule, this._Network, srcKeys);

                    Network network = worker.ReadRelatedData();
                    if (NetworkDedupSetting.Exists(network.NetworkName))
                    {
                        NetworkDedupSetting dedupSetting = NetworkDedupSetting.Read(network.NetworkName);
                        NetworkDedupWorker dedupWorker = new NetworkDedupWorker(network, dedupSetting);
                        network = dedupWorker.DedupNetwork();
                    }
                    network.Save();
                    MyData.ReadAllNetworks();
                    if (this.NetworkUpdated != null)
                        this.NetworkUpdated(network);

                    if (this.Parent is Window)
                    {
                        ((Window)this.Parent).Close();
                    }
                }
                catch (Exception ex)
                {
                    ExceptionHandler.HandlesException(ex);
                    MessageBox.Show(ex.Message);
                }
            }


        }
    }
}
